package com.topcheer.provider.rbac.rest;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.topcheer.provider.rbac.entity.RoleResource;
import com.topcheer.provider.rbac.service.RoleResourceService;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.pagehelper.PageInfo;
import com.topcheer.common.base.util.JsonMapper;
import com.topcheer.common.base.vo.BaseResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/roleResource")
@Api(description = "角色资源关联信息")
public class RoleResourceController{

    @Autowired
    RoleResourceService roleResourceService;

    @PostMapping("")
    @ApiOperation(value="新增角色资源关联", notes="新增角色资源关联", produces="application/json")
    public BaseResponse<RoleResource> insert(@RequestBody RoleResource entity){
        roleResourceService.insert(entity);
        return new BaseResponse<RoleResource>().code(HttpStatus.OK.value()).msg(HttpStatus.OK.getReasonPhrase());
    }

    @GetMapping("/{id}")
    @ApiOperation(value="根据id获取角色资源关联", notes="根据id获取角色资源关联", produces="application/json")
    public BaseResponse<RoleResource> selectOne(@PathVariable("id") Long id){
        Object o = roleResourceService.selectOne(id);
        return new BaseResponse<RoleResource>().data((RoleResource) o);
    }

    @GetMapping("/page")
    @ApiOperation(value="获取角色资源关联分页信息", notes="获取角色资源关联分页信息", produces="application/json")
    public BaseResponse<PageInfo> page(String page){
        JsonMapper jsonMapper = JsonMapper.defaultMapper();
        Map map = jsonMapper.fromJson(page, Map.class);
        PageInfo pageInfo = roleResourceService.selectPage(map);
        return new BaseResponse<PageInfo>().data(pageInfo);
    }

    @PutMapping("")
    @ApiOperation(value="更新角色资源关联", notes="更新角色资源关联", produces="application/json")
    public BaseResponse<RoleResource> update(@RequestBody RoleResource entity){
        roleResourceService.update(entity);
        return new BaseResponse<RoleResource>();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value="删除角色资源关联", notes="删除角色资源关联", produces="application/json")
    public BaseResponse<RoleResource> delete(@PathVariable("id") Long id){
        roleResourceService.deleteById(id);
        return new BaseResponse<RoleResource>();
    }

    @GetMapping("/all")
    @ApiOperation(value="获取所有角色资源关联", notes="获取所有角色资源关联", produces="application/json")
    public BaseResponse<List> selectAll(){
        List list = roleResourceService.selectAll();
        return new BaseResponse<List>().data(list);
    }
}
